﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WorldOnFire
{
    public class CHomeworld : ABCObject
    {
        #region Homeworld Data

        CBuildable[] m_Buildable;
        int[] m_nTargetIndex;
        int[] m_nShieldIndex;
        bool[] m_bGravityBeamActive;

        const float MINIMUM_SAFE_DISTANCE = 100.0f;

        #endregion Homeworld Data

        #region Initialization

        // TODO: RRRRRRRRAAAAAAAAGGEEEE
        bool m_bInitialized;

        public override void Initialize(int _nObjectCount)
        {
            // Initialize arrays
            m_Buildable = new CBuildable[_nObjectCount];

            m_nTargetIndex = new int[_nObjectCount];
            m_nShieldIndex = new int[_nObjectCount];
            m_bGravityBeamActive = new bool[_nObjectCount];

            // Initialize base members
            base.Initialize(_nObjectCount);

            // Set rendering data
            m_nTechniqueID = (int)CRenderingManager.eObjectTechnique.CELESTIAL_BODY;
            m_bOverlay = true;

            for (int iHomeworld = 0; iHomeworld < _nObjectCount; ++iHomeworld)
            {
                // TODO: Change. Fix radius thing
                m_Buildable[iHomeworld] = new CBuildable();
                m_Buildable[iHomeworld].Initialize(50.0f);
                m_fRadius[iHomeworld] = 50.0f;
                m_nTargetIndex[iHomeworld] = 0;
            }

            // TODO: MOVE
           // m_eShaderID = CRenderingManager.eEffectID.
        }

        public override void LoadContent()
        {
            // Load the model
            m_eModelID = CRenderingManager.eModelID.CELESTIAL_BODY;

            // Load the texture
            m_eTextureID = CRenderingManager.eTextureID.HOMEWORLD_1;

            // Load base content
            base.LoadContent();
        }

        #endregion Initialization

        #region Update and Render

        public override void Update(float _fDeltaTime)
        {
            // TODO: FFFFFFFFFUUUUUUUUUUUUUUU
            if (false && m_bInitialized == false)
            {
                m_Buildable[0].CreateTurret(ABCTurret.eTurretType.REPEATER);
                m_Buildable[0].CreateTurret(ABCTurret.eTurretType.CANNON);
                m_Buildable[0].CreateTurret(ABCTurret.eTurretType.EMP);

                m_bInitialized = true;
            }

            // For each homeworld
            for (int iHomeworld = 0; iHomeworld < m_nObjectCount; ++iHomeworld)
            {
                // Update buildable
                m_Buildable[iHomeworld].Update(_fDeltaTime, m_mTransform[iHomeworld].Translation);

                // Rotate around the Y axis
                float fRotationSpeed = (float)Math.PI / 50.0f;
                Matrix mSpin = Matrix.CreateRotationY(fRotationSpeed * _fDeltaTime);
                m_mTransform[iHomeworld] = m_mTransform[iHomeworld] * mSpin;
            }
            // Store the satellite
            CSatellite Satellite = s_ObjectManager.GetCelestialBody();

            // For each satellite
            for (int iHomeworld = 0; iHomeworld < m_nObjectCount; ++iHomeworld)
            {
                // For every satellite
                for (int iSatellite = 0; iSatellite < Satellite.GetObjectCount(); ++iSatellite)
                {
                    // Find the vector between
                    Vector3 vBetween = Satellite.GetPosition(iSatellite) - this.GetPosition(iHomeworld);

                    // Find the combined radius
                    float fCombinedRadius = m_fRadius[iHomeworld] + Satellite.GetRadius(iSatellite);

                    // If the distance between is less than the minimum safe distance added to the radius
                    if (vBetween.LengthSquared() < (MINIMUM_SAFE_DISTANCE + fCombinedRadius)
                        * (MINIMUM_SAFE_DISTANCE + fCombinedRadius))
                    {
                        // Correct each satellite's position
                        float fCorrectionDistance = vBetween.Length();
                        vBetween /= fCorrectionDistance;
                        fCorrectionDistance = MINIMUM_SAFE_DISTANCE + fCombinedRadius - fCorrectionDistance;

                        Satellite.SetPosition(iSatellite, Satellite.GetPosition(iSatellite) + fCorrectionDistance * vBetween);
                    }
                }
            }
            
            // Update base members
            base.Update(_fDeltaTime);
        }

        #endregion Update and Render
    }
}
